package com.business.controller.interfaced;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.business.service.interfaced.ApplyForInManager;
import com.business.service.interfaced.BusinessInManager;
import com.business.service.interfaced.ProductInManager;
import com.system.controller.base.BaseController;
import com.system.redis.RedisDao;
import com.system.util.AppUtil;
import com.system.util.Const;
import com.system.util.FTPUtils;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;
import com.system.util.UpdImgUtils;

/**
 * 商家入驻接口
 * @Function 
 * @Author zhaozhenhua
 * @Date 2018年2月1日
 */
@Controller
@RequestMapping(value="/business")
public class BusinessInController extends BaseController{
	
	@Resource(name="applyforInService")
	private ApplyForInManager applyforInService;
	
	@Resource(name="productInService")
	private ProductInManager productInService;
	
	@Resource(name="businessInService")
	private BusinessInManager businessInService;
	
	@Resource(name = "redisDaoImpl")
	private RedisDao redisDaoImpl;
	
	//经营者身份照片地址
	private static String APPLYFOR_IDCARDIMAGE="";
	
	//实体店铺照片地址
	private static String APPLYFOR_IMAGES="";
	
	//店铺营业执照图片地址
	private static String APPLYFOR_CHARTERIMAGE="";
	
	//店铺头像
	private static String APPLYFOR_SHOWIMAGE="";
	
	
	/**
	 * 商家修改入驻信息
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月1日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/updapplybusiness")
	@ResponseBody
	public Object updApplyBusiness(){
		logBefore(logger, "商家修改入驻信息");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPDAPPLYBUSINESS", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getApplyBusiness", pd)){	//检查参数
					PageData p = applyforInService.findById(pd);
					if(APPLYFOR_IDCARDIMAGE!=null&&APPLYFOR_IDCARDIMAGE!=""){
						UpdImgUtils.updImgsMethods(p, pd,"APPLYFOR_IDCARDIMAGE",APPLYFOR_IDCARDIMAGE.substring(1), "businessimgs/identifyimgs");
					}
					if(APPLYFOR_IMAGES!=null&&APPLYFOR_IMAGES!=""){
						UpdImgUtils.updImgsMethods(p, pd,"APPLYFOR_IMAGES",APPLYFOR_IMAGES.substring(1), "businessimgs/shopimgs");
					}
					if(APPLYFOR_CHARTERIMAGE!=null&&APPLYFOR_CHARTERIMAGE!=""){
						UpdImgUtils.updImgsMethods(p, pd,"APPLYFOR_CHARTERIMAGE",APPLYFOR_CHARTERIMAGE.substring(1), "businessimgs/licenceimgs");
					}
					UpdImgUtils.updImgMethod(p, pd,APPLYFOR_SHOWIMAGE,"APPLYFOR_SHOWIMAGE","businessimgs/shopheadimgs");
					applyforInService.edit(pd);
					APPLYFOR_IDCARDIMAGE="";
					APPLYFOR_IMAGES="";
					APPLYFOR_CHARTERIMAGE="";
					APPLYFOR_SHOWIMAGE="";
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *  获取商家入驻信息
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月1日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getapplybusiness")
	@ResponseBody
	public Object getApplyBusiness(){
		logBefore(logger, "获取商家入驻信息");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETAPPLYBUSINESS", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getApplyBusiness", pd)){	//检查参数
					PageData p = applyforInService.findById(pd);
					ToolsUtil.putImgstoArray(p, "APPLYFOR_IDCARDIMAGE");
					ToolsUtil.putImgstoArray(p, "APPLYFOR_IMAGES");
					ToolsUtil.putImgstoArray(p, "APPLYFOR_CHARTERIMAGE");
					map.put("applyinfo", p);
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *  获取商家入驻信息列表列表全部(是否审核（0表示未审核，1审核通过，2审核驳回）)
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月1日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getapplybusilist")
	@ResponseBody
	public Object getApplyBusiList(){
		logBefore(logger, "获取商家入驻信息列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETAPPLYBUSILIST", pd.getString("FKEY"))){	//检验请求key值是否合法
					List<PageData> applylist = applyforInService.listAll(pd);
					for (PageData p : applylist) {
						ToolsUtil.putImgstoArray(p, "APPLYFOR_IDCARDIMAGE");
						ToolsUtil.putImgstoArray(p, "APPLYFOR_IMAGES");
						ToolsUtil.putImgstoArray(p, "APPLYFOR_CHARTERIMAGE");
					}
					map.put("applylist", applylist);
					result =  (null==applylist) ?"02":"01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	 /**
	  *  上传店铺头像
	   * @Description
	   * @Author zhaozhenhua
	   * @date   2018年2月1日
	   * @param pd
	   * @return
	   * @throws Exception
	  */
	@RequestMapping("/upshopheadimg")
	@ResponseBody
	public synchronized Object upShopHeadImg(HttpServletRequest req, MultipartHttpServletRequest multiReq,
			HttpSession session) throws Exception {
		logBefore(logger, "上传店铺头像");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("UPBUSINESSPORT", pd.getString("FKEY"))) { // 检验请求key值是否合法
					// 获取上传文件的路径
					String uploadFilePath = multiReq.getFile("file").getOriginalFilename();
					// 截取上传文件的后缀
					String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1,
							uploadFilePath.length());
					// 用UUID来解决同名文件
					String upload = UUID.randomUUID().toString().replace("-", "");
					// 获取传输过来的Input流
					InputStream is = multiReq.getFile("file").getInputStream();
					// 根据txt文件读取ftp工具类上传需要的参数
					String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
					String[] split = updeploy.split(",");
					// 第一个参数为 图片服务器的地址
					// 第二个参数为 图片服务器接口
					// 第三/四个参数为 FTP服务器的登录用户名和密码
					String path = "businessimgs/shopheadimgs";
					FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path,
							upload + "." + uploadFileSuffix, is);
					// 通过工具类获取图片服务器地址
					String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
				    APPLYFOR_SHOWIMAGE = imgpath + path + "/" + upload + "." + uploadFileSuffix;
					result = "01";
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 上传经营者身份照片（已废弃）
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年1月31日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/upidentifyimg")
	public Object upIdentifyImg(MultipartHttpServletRequest mreq,HttpServletResponse resp,HttpServletRequest request){
		logBefore(logger, "上传经营者身份照片（已废弃）");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPIDENTIFYIMG", pd.getString("FKEY"))){	//检验请求key值是否合法
			        	String filePath = "";
						  Iterator<String> iterator = mreq.getFileNames();
						    while (iterator.hasNext()) { 
						    	MultipartFile file = mreq.getFile(iterator.next());
						    	 String imgname = UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
					            	InputStream in = file.getInputStream();
										resp.setCharacterEncoding("UTF-8");
										resp.getWriter().write(filePath+";");
										//根据txt文件读取ftp工具类上传需要的参数
										String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
										String[] split = updeploy.split(",");
										//第一个参数为  图片服务器的地址
										//第二个参数为  图片服务器接口
										//第三/四个参数为  FTP服务器的登录用户名和密码
										//通过工具类获取图片服务器地址
										String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
										String path="businessimgs/identifyimgs";
										FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3],path, imgname, in);
										APPLYFOR_IDCARDIMAGE+=","+imgpath+path+"/"+imgname;
								}
						    result = "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * app端商家入驻
	 * @Author zhaozhenhua
	 * @date   2017年12月6日
	 * @return
	 */
	@RequestMapping(value="/appbusinessin")
	@ResponseBody
	public synchronized Object appBusinessIn(MultipartHttpServletRequest mreq,HttpServletResponse resp,HttpServletRequest request,String FKEY,
			String CATEGORY_ID,String  APPLYFOR_NAME,String APPLYFOR_PROVINCE,String  APPLYFOR_CITY,String  APPLYFOR_ADDRESS,String  APPLYFOR_LONGITUDE
			,String  APPLYFOR_LATITUDE,String  APPLYFOR_TEL,String  APPLYFOR_IDCARD,String  APPLYFOR_CHARTERNUM,String  APPLYFOR_CHARTERNAME,String  APPLYFOR_LEGALPERSON
			,String  APPLYFOR_PHONE,String  APPLY_COUNTY,String  HONOURUSER_ID,String  APPLY_OPERRATE,String IDENTIFY_CODE){
		logBefore(logger, "app端商家入驻");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("FKEY", FKEY);
		String result = "00";
		System.out.println("IDENTIFY_CODE验证码:"+IDENTIFY_CODE);
		try{
			if(Tools.checkKey("APPBUSINESSIN", pd.getString("FKEY"))){	//检验请求key值是否合法
				//if(AppUtil.checkParam("addApplyBusiness", pd)){	//检查参数
				//从redis中获取验证码
					String ALI_CODE = redisDaoImpl.get(Const.ALI_CODE+APPLYFOR_PHONE);
					System.out.println("阿里云验证码:"+ALI_CODE);
					if(Tools.isEmpty(ALI_CODE)||!IDENTIFY_CODE.equals(ALI_CODE)){
						map.put("pd", "验证码错误或过期");
						return AppUtil.returnObject(new PageData(), map);
					}
					pd.put("CATEGORY_ID", CATEGORY_ID);
					pd.put("APPLYFOR_NAME", APPLYFOR_NAME);
					pd.put("APPLYFOR_PROVINCE", APPLYFOR_PROVINCE);
					pd.put("APPLYFOR_CITY", APPLYFOR_CITY);
					pd.put("APPLYFOR_ADDRESS", APPLYFOR_ADDRESS);
					pd.put("APPLYFOR_LONGITUDE", APPLYFOR_LONGITUDE);
					pd.put("APPLYFOR_LATITUDE", APPLYFOR_LATITUDE);
					pd.put("APPLYFOR_TEL", APPLYFOR_TEL);
					pd.put("APPLYFOR_IDCARD", APPLYFOR_IDCARD);
					pd.put("APPLYFOR_CHARTERNUM", APPLYFOR_CHARTERNUM);
					pd.put("APPLYFOR_CHARTERNAME", APPLYFOR_CHARTERNAME);
					pd.put("APPLYFOR_LEGALPERSON", APPLYFOR_LEGALPERSON);
					pd.put("APPLYFOR_PHONE", APPLYFOR_PHONE);
					pd.put("APPLY_COUNTY", APPLY_COUNTY);
					pd.put("HONOURUSER_ID", HONOURUSER_ID);
					pd.put("APPLY_OPERRATE", APPLY_OPERRATE);
					List<MultipartFile> fileList1 = mreq.getFiles("APPLYFOR_IDCARDIMAGE");
					List<MultipartFile> fileList2 = mreq.getFiles("APPLYFOR_IMAGES");
					List<MultipartFile> fileList3 = mreq.getFiles("APPLYFOR_CHARTERIMAGE");
					// 创建一个通用的多部分解析器.  
			        //CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
			        // 设置编码  
			        //commonsMultipartResolver.setDefaultEncoding("utf-8");  
			        // 判断是否有文件上传  
			        //if (commonsMultipartResolver.isMultipart(request)){
			        	for (MultipartFile file : fileList1) {
			        		//String filePath = "";
			        		if (file!=null&&file.getOriginalFilename().length()!=0&&file.getSize()!= 0) {
			        			//获得UUID之后的文件名
			        			String imgname=UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
			        			InputStream in = file.getInputStream();
			        			//获得文件全名
			        			try {
			        				resp.setCharacterEncoding("UTF-8");
			        				//resp.getWriter().write(filePath+";");
			        				//根据txt文件读取ftp工具类上传需要的参数
			        				String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
			        				String[] split = updeploy.split(",");
			        				//第一个参数为  图片服务器的地址
			        				//第二个参数为  图片服务器接口
			        				//第三/四个参数为  FTP服务器的登录用户名和密码
			        				//通过工具类获取图片服务器地址
			        				String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
			        				//String path="businessimgs/identifyimgs";
			        				String path="test";
			        				FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path, imgname, in);
			        				APPLYFOR_IDCARDIMAGE+=","+imgpath+path+"/"+imgname;
			        			} catch (Exception e) {
			        				e.printStackTrace();
			        			}
			        		}
			            }
			        	for (MultipartFile file : fileList2) {
			        		//String filePath = "";
			        		if (file!=null&&file.getOriginalFilename().length()!=0&&file.getSize()!= 0) {
			        			//获得UUID之后的文件名
			        			String imgname=UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
			        			InputStream in = file.getInputStream();
			        			//获得文件全名
			        			try {
			        				resp.setCharacterEncoding("UTF-8");
			        				//resp.getWriter().write(filePath+";");
			        				//根据txt文件读取ftp工具类上传需要的参数
			        				String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
			        				String[] split = updeploy.split(",");
			        				//第一个参数为  图片服务器的地址
			        				//第二个参数为  图片服务器接口
			        				//第三/四个参数为  FTP服务器的登录用户名和密码
			        				//通过工具类获取图片服务器地址
			        				String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
			        				//String path="businessimgs/shopimgs";
			        				String path="test";
			        				FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path, imgname, in);
			        				APPLYFOR_IMAGES+=","+imgpath+path+"/"+imgname;
			        			} catch (Exception e) {
			        				e.printStackTrace();
			        			}
			        		}
			            }
			        	for (MultipartFile file : fileList3) {
			        		//String filePath = "";
			        		if (file!=null&&file.getOriginalFilename().length()!=0&&file.getSize()!= 0) {
			        			//获得UUID之后的文件名
			        			String imgname=UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
			        			InputStream in = file.getInputStream();
			        			//获得文件全名
			        			try {
			        				resp.setCharacterEncoding("UTF-8");
			        				//resp.getWriter().write(filePath+";");
			        				//根据txt文件读取ftp工具类上传需要的参数
			        				String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
			        				String[] split = updeploy.split(",");
			        				//第一个参数为  图片服务器的地址
			        				//第二个参数为  图片服务器接口
			        				//第三/四个参数为  FTP服务器的登录用户名和密码
			        				//通过工具类获取图片服务器地址
			        				String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
			        				//String path="businessimgs/licenceimgs";
			        				String path="test";
			        				FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path, imgname, in);
			        				APPLYFOR_CHARTERIMAGE+=","+imgpath+path+"/"+imgname;
			        			} catch (Exception e) {
			        				e.printStackTrace();
			        			}
			        		}
			            }
						//发布时间
						Date date = new Date();
						String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
						pd.put("APPLYFOR_TIME", format);
						pd.put("APPLYFOR_ID", this.getSnowId());
						if(APPLYFOR_IDCARDIMAGE!=null&&APPLYFOR_IDCARDIMAGE!=""){
							pd.put("APPLYFOR_IDCARDIMAGE", APPLYFOR_IDCARDIMAGE.substring(1));
							System.out.println("APPLYFOR_IDCARDIMAGE:"+APPLYFOR_IDCARDIMAGE);
						}
						if(APPLYFOR_IMAGES!=null&&APPLYFOR_IMAGES!=""){
							pd.put("APPLYFOR_IMAGES", APPLYFOR_IMAGES.substring(1));
							System.out.println("APPLYFOR_IMAGES:"+APPLYFOR_IMAGES);
						}
						if(APPLYFOR_CHARTERIMAGE!=null&&APPLYFOR_CHARTERIMAGE!=""){
							pd.put("APPLYFOR_CHARTERIMAGE", APPLYFOR_CHARTERIMAGE.substring(1));
							System.out.println("APPLYFOR_CHARTERIMAGE:"+APPLYFOR_CHARTERIMAGE);
						}
						pd.put("APPLYFOR_ISPASS", "0");  //是否审核（0表示未审核，1审核通过，2审核驳回）
						applyforInService.save(pd);
						APPLYFOR_IDCARDIMAGE="";
						APPLYFOR_IMAGES="";
						APPLYFOR_CHARTERIMAGE="";
					result = (null == pd) ?  "02" :  "01";
				/*}else {
					result = "03";
				}*/
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台店铺添加或修改品牌
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月26日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/updshopcategory")
	@ResponseBody
	public Object updShopCategory(){
		logBefore(logger, "后台店铺添加或修改品牌");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPDSHOPCATEGORY", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("updShopCategory", pd)){	//检查参数
				productInService.updShopCategory(pd);
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/***
	 * 后台根据商家id获取店铺信息
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月1日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getbusinessinfo")
	@ResponseBody
	public Object getBusinessInfo(){
		logBefore(logger, "后台根据商家id获取店铺信息");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETBUSINESSIFNO", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCouponListById", pd)){	//检查参数
					PageData p = businessInService.getBusinessInfo(pd);
					if(p!=null){
						p.remove("PRODUCTIMGS");
						map.put("pd", p);
					}
					result = (p==null)?"02" :"01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台商家修改审核信息
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月3日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/updapplyinfo")
	@ResponseBody
	public synchronized Object updApplyInfo(MultipartHttpServletRequest mreq,HttpServletResponse resp,HttpServletRequest request,String APPLYFOR_ID,
			String CATEGORY_ID,String  APPLYFOR_NAME,String APPLYFOR_PROVINCE,String  APPLYFOR_CITY,String  APPLYFOR_ADDRESS,String  APPLYFOR_LONGITUDE
			,String  APPLYFOR_LATITUDE,String  APPLYFOR_TEL,String  APPLYFOR_IDCARD,String  APPLYFOR_CHARTERNUM,String  APPLYFOR_CHARTERNAME,String  APPLYFOR_LEGALPERSON
			,String  APPLYFOR_PHONE,String  APPLY_COUNTY,String  HONOURUSER_ID,String  APPLY_OPERRATE){
		logBefore(logger, "后台商家修改审核信息");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		System.out.println("APPLYFOR_ID申请id:"+APPLYFOR_ID);
		try{
			if(Tools.checkKey("UPDAPPLYINFO", pd.getString("FKEY"))){	//检验请求key值是否合法
				//if(AppUtil.checkParam("addApplyBusiness", pd)){	//检查参数
					pd.put("CATEGORY_ID", CATEGORY_ID);
					pd.put("APPLYFOR_NAME", APPLYFOR_NAME);
					pd.put("APPLYFOR_PROVINCE", APPLYFOR_PROVINCE);
					pd.put("APPLYFOR_CITY", APPLYFOR_CITY);
					pd.put("APPLYFOR_ADDRESS", APPLYFOR_ADDRESS);
					pd.put("APPLYFOR_LONGITUDE", APPLYFOR_LONGITUDE);
					pd.put("APPLYFOR_LATITUDE", APPLYFOR_LATITUDE);
					pd.put("APPLYFOR_TEL", APPLYFOR_TEL);
					pd.put("APPLYFOR_IDCARD", APPLYFOR_IDCARD);
					pd.put("APPLYFOR_CHARTERNUM", APPLYFOR_CHARTERNUM);
					pd.put("APPLYFOR_CHARTERNAME", APPLYFOR_CHARTERNAME);
					pd.put("APPLYFOR_LEGALPERSON", APPLYFOR_LEGALPERSON);
					pd.put("APPLYFOR_PHONE", APPLYFOR_PHONE);
					pd.put("APPLY_COUNTY", APPLY_COUNTY);
					pd.put("HONOURUSER_ID", HONOURUSER_ID);
					pd.put("APPLY_OPERRATE", APPLY_OPERRATE);
					pd.put("APPLYFOR_ID", APPLYFOR_ID);
					List<MultipartFile> fileList1 = mreq.getFiles("APPLYFOR_IDCARDIMAGE");
					List<MultipartFile> fileList2 = mreq.getFiles("APPLYFOR_IMAGES");
					List<MultipartFile> fileList3 = mreq.getFiles("APPLYFOR_CHARTERIMAGE");
					// 创建一个通用的多部分解析器.  
			        //CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
			        // 设置编码  
			        //commonsMultipartResolver.setDefaultEncoding("utf-8");  
			        // 判断是否有文件上传  
			        //if (commonsMultipartResolver.isMultipart(request)){
					// 获取上传文件的路径
					String APPLYFOR_SHOWIMAGE=null;
					if(true){
						APPLYFOR_SHOWIMAGE = FTPUtils.upSingleImg(mreq, "APPLYFOR_SHOWIMAGE", "businessimgs/shopheadimgs");
					}
			        	for (MultipartFile file : fileList1) {
			        		//String filePath = "";
			        		if (file!=null&&file.getOriginalFilename().length()!=0&&file.getSize()!= 0) {
			        			//获得UUID之后的文件名
			        			String imgname=UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
			        			InputStream in = file.getInputStream();
			        			//获得文件全名
			        			try {
			        				resp.setCharacterEncoding("UTF-8");
			        				//resp.getWriter().write(filePath+";");
			        				//根据txt文件读取ftp工具类上传需要的参数
			        				String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
			        				String[] split = updeploy.split(",");
			        				//第一个参数为  图片服务器的地址
			        				//第二个参数为  图片服务器接口
			        				//第三/四个参数为  FTP服务器的登录用户名和密码
			        				//通过工具类获取图片服务器地址
			        				String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
			        				String path="businessimgs/identifyimgs";
			        				FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path, imgname, in);
			        				APPLYFOR_IDCARDIMAGE+=","+imgpath+path+"/"+imgname;
			        			} catch (Exception e) {
			        				e.printStackTrace();
			        			}
			        		}
			            }
			        	for (MultipartFile file : fileList2) {
			        		//String filePath = "";
			        		if (file!=null&&file.getOriginalFilename().length()!=0&&file.getSize()!= 0) {
			        			//获得UUID之后的文件名
			        			String imgname=UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
			        			InputStream in = file.getInputStream();
			        			//获得文件全名
			        			try {
			        				resp.setCharacterEncoding("UTF-8");
			        				//resp.getWriter().write(filePath+";");
			        				//根据txt文件读取ftp工具类上传需要的参数
			        				String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
			        				String[] split = updeploy.split(",");
			        				//第一个参数为  图片服务器的地址
			        				//第二个参数为  图片服务器接口
			        				//第三/四个参数为  FTP服务器的登录用户名和密码
			        				//通过工具类获取图片服务器地址
			        				String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
			        				String path="businessimgs/shopimgs";
			        				FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path, imgname, in);
			        				APPLYFOR_IMAGES+=","+imgpath+path+"/"+imgname;
			        			} catch (Exception e) {
			        				e.printStackTrace();
			        			}
			        		}
			            }
			        	for (MultipartFile file : fileList3) {
			        		//String filePath = "";
			        		if (file!=null&&file.getOriginalFilename().length()!=0&&file.getSize()!= 0) {
			        			//获得UUID之后的文件名
			        			String imgname=UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
			        			InputStream in = file.getInputStream();
			        			//获得文件全名
			        			try {
			        				resp.setCharacterEncoding("UTF-8");
			        				//resp.getWriter().write(filePath+";");
			        				//根据txt文件读取ftp工具类上传需要的参数
			        				String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
			        				String[] split = updeploy.split(",");
			        				//第一个参数为  图片服务器的地址
			        				//第二个参数为  图片服务器接口
			        				//第三/四个参数为  FTP服务器的登录用户名和密码
			        				//通过工具类获取图片服务器地址
			        				String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
			        				String path="businessimgs/licenceimgs";
			        				FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path, imgname, in);
			        				APPLYFOR_CHARTERIMAGE+=","+imgpath+path+"/"+imgname;
			        			} catch (Exception e) {
			        				e.printStackTrace();
			        			}
			        		}
			            }
						//发布时间
						Date date = new Date();
						String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
						pd.put("APPLYFOR_TIME", format);
						if(APPLYFOR_SHOWIMAGE!=null&&APPLYFOR_SHOWIMAGE!=""){
							pd.put("APPLYFOR_SHOWIMAGE", APPLYFOR_SHOWIMAGE);
						}
						if(APPLYFOR_IDCARDIMAGE!=null&&APPLYFOR_IDCARDIMAGE!=""){
							pd.put("APPLYFOR_IDCARDIMAGE", APPLYFOR_IDCARDIMAGE.substring(1));
							//System.out.println("APPLYFOR_IDCARDIMAGE:"+APPLYFOR_IDCARDIMAGE);
						}
						if(APPLYFOR_IMAGES!=null&&APPLYFOR_IMAGES!=""){
							pd.put("APPLYFOR_IMAGES", APPLYFOR_IMAGES.substring(1));
							//System.out.println("APPLYFOR_IMAGES:"+APPLYFOR_IMAGES);
						}
						if(APPLYFOR_CHARTERIMAGE!=null&&APPLYFOR_CHARTERIMAGE!=""){
							pd.put("APPLYFOR_CHARTERIMAGE", APPLYFOR_CHARTERIMAGE.substring(1));
							//System.out.println("APPLYFOR_CHARTERIMAGE:"+APPLYFOR_CHARTERIMAGE);
						}
						//pd.put("APPLYFOR_ISPASS", "0");  //是否审核（0表示未审核，1审核通过，2审核驳回）
						applyforInService.edit(pd);
						APPLYFOR_IDCARDIMAGE="";
						APPLYFOR_IMAGES="";
						APPLYFOR_CHARTERIMAGE="";
					result = (null == pd) ?  "02" :  "01";
				/*}else {
					result = "03";
				}*/
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 后台修改或者添加电商商铺首页的宣传图和它关联的商品
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月12日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/updbusiadimg")
	@ResponseBody
	public synchronized Object updBusiADImg(MultipartHttpServletRequest mreq,HttpServletResponse resp,HttpServletRequest request,String FIRST_AD_PROID,
			String  SECOND_AD_PROID,String  BUSINESS_ID){
		logBefore(logger, "后台修改或者添加电商商铺首页的宣传图和它关联的商品");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		String FIRST_AD_IMG="";
		String SECONG_AD_IMG="";
		try{
			if(Tools.checkKey("UPDBUSIADIMG", pd.getString("FKEY"))){	//检验请求key值是否合法
				//if(AppUtil.checkParam("addApplyBusiness", pd)){	//检查参数
					pd.put("FIRST_AD_IMG", FIRST_AD_IMG);
					pd.put("FIRST_AD_PROID", FIRST_AD_PROID);
					pd.put("SECONG_AD_IMG", SECONG_AD_IMG);
					pd.put("SECOND_AD_PROID", SECOND_AD_PROID);
					pd.put("BUSINESS_ID", BUSINESS_ID);
					// 创建一个通用的多部分解析器.  
			        //CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
			        // 设置编码  
			        //commonsMultipartResolver.setDefaultEncoding("utf-8");  
			        // 判断是否有文件上传  
			        //if (commonsMultipartResolver.isMultipart(request)){
					// 获取上传文件的路径
					PageData p = businessInService.getBusiADImgById(pd);
					if(mreq.getFile("FIRST_AD_IMG").isEmpty()){}
					else{
					    FIRST_AD_IMG = FTPUtils.upSingleImg(mreq, "FIRST_AD_IMG", "businessimgs/firstimg");
					    UpdImgUtils.updImgMethod(p, pd, FIRST_AD_IMG, "FIRST_AD_IMG", "businessimgs/firstimg");
					}
					if(mreq.getFile("SECONG_AD_IMG").isEmpty()){}
					else{
						SECONG_AD_IMG = FTPUtils.upSingleImg(mreq, "SECONG_AD_IMG", "businessimgs/secondimg");
						UpdImgUtils.updImgMethod(p, pd, SECONG_AD_IMG, "SECONG_AD_IMG", "businessimgs/secondimg");
					}
                    productInService.updShopCategory(pd);
					result = (null == p) ?  "02" :  "01";
				/*}else {
					result = "03";
				}*/
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 后台获取电商商铺首页的宣传图和它关联的商品
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月12日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getbusiadimgbyid")
	@ResponseBody
	public Object getBusiADImgById(){
		logBefore(logger, "后台获取电商商铺首页的宣传图和它关联的商品");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETBUSIADIMGBYID", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCouponListById", pd)){	//检查参数
					PageData p = businessInService.getBusiADImgById(pd);
					pd.put("PRODUCT_ID", p.get("FIRST_AD_PROID"));
					PageData p1 = productInService.findById(pd);
					p.put("PROTITLE1", p1.get("PROTITLE"));
					pd.put("PRODUCT_ID", p.get("SECOND_AD_PROID"));
					PageData p2 = productInService.findById(pd);
					p.put("PROTITLE2", p2.get("PROTITLE"));
					if(p!=null){
						map.put("pd", p);
					}
					//返回01为添加成功并返回
					result = (p==null)? "02":"01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * app端获取店铺首页信息
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月13日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getbusihomepage")
	@ResponseBody
	public Object getBusiHomePage(){
		logBefore(logger, "app端获取店铺首页信息");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETBUSIHOMEPAGE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCouponListById", pd)){	//检查参数
					PageData tow_img = businessInService.getBusiADImgById(pd);
					pd.put("IS_NEW", 1);
					List<PageData> news = productInService.getProdListById(pd);
					pd.put("IS_NEW", 0);
					List<PageData> proList = productInService.getProdListById(pd);
					PageData busiInfo = businessInService.getBusinessLogo(pd);
					if(busiInfo!=null){
						map.put("tow_img", tow_img);
						map.put("news", news);
						map.put("proList", proList);
						map.put("busiInfo", busiInfo);
					}
					//返回01为添加成功并返回
					result = (busiInfo==null)? "02":"01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	

}
